//
//  CaculatorViewController.m
//  Caculator
//
//  Created by Lion User on 03/07/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CaculatorViewController.h"
#import "CaculatorBrain.h"

@interface CaculatorViewController()
@property (nonatomic) bool userIsInTheMiddleOfEnteringANumber;
@property (nonatomic,strong) CaculatorBrain *brain;
@end

@implementation CaculatorViewController

@synthesize display=_display;
@synthesize userIsInTheMiddleOfEnteringANumber=_userIsInTheMiddleOfEnteringANumber;
@synthesize brain=_brain;

-(CaculatorBrain *)brain{
    if(!_brain) _brain=[[CaculatorBrain alloc] init];
    return _brain;
}

- (IBAction)digitPressed:(UIButton*)sender {
    NSString *digit = [sender currentTitle];
//    NSLog(@"digit pressed = %@", digit);
    if(self.userIsInTheMiddleOfEnteringANumber){
        self.display.text=[self.display.text stringByAppendingString:digit];
    }else{
        self.display.text =digit;
        self.userIsInTheMiddleOfEnteringANumber=YES;
    }
}

- (IBAction)enterPressed{
    NSLog(@"Entered string =%@",self.display.text);
    [self.brain pushOperand:[self.display.text doubleValue]];
    self.userIsInTheMiddleOfEnteringANumber = NO;
}

- (IBAction)operationPressed:(UIButton *)sender {    
    if(self.userIsInTheMiddleOfEnteringANumber)[self enterPressed];
    double result=[self.brain performOperation:sender.currentTitle];
    NSString *resultString=[NSString stringWithFormat:@"%g",result];
    self.display.text =resultString;
}

@end
